// Appendix3.3
log using Appendix3.3.log, replace
set maxvar 30000
use "C:\Users\sbstjp\OneDrive - Cardiff University\BES2024_W29_Panel_v29.1.dta" // Fieldhouse, E., J. Green, G. Evans, J. Mellon, C. Prosser, J. Bailey, R. de Geus, H. Schmitt, C. van der Eijk, J. Griffiths, & S. Perrett. (2024) British Election Study Internet Panel Waves 1-29. DOI: 10.5255/UKDA-SN-8202-2// Accessed on March 11 2025.

// Social justice scale
* Clean "don't know" responses
foreach var in cwTransW25 cwAuthorsW25 cwLanguageW25 cwTrainingW25 {
    replace `var' = . if `var' == 9999
}

*Reverse variable so social justice coded high
foreach var in cwLanguageW25 cwTrainingW25 {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in cwTransW25 cwAuthorsW25 rcwLanguageW25 rcwTrainingW25 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in scwTransW25 scwAuthorsW25 srcwLanguageW25 srcwTrainingW25 {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in scwTransW25 scwAuthorsW25 srcwLanguageW25 srcwTrainingW25 {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen socJusValues = .
replace socJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Liberalism scale reverse
qui sum al_scaleW23 
local max_value = r(max)
gen reversed_al_scaleW23 = `max_value' - al_scaleW23

// LifeSatisfaction
replace lifeSatW23=. if lifeSatW23==9999 

*Standardize lifesat from 1-2, so it's like other dependent variables in the book
foreach var in lifeSatW23 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

rename slifeSatW23 LifeSat

//Demographics
*Recode, delete missing values and rename
replace p_gross_householdW23=. if inlist(p_gross_householdW23, 16, 17) 

gen ReligiousAtt=.
replace ReligiousAtt=1 if churchAttendanceW23==6
replace ReligiousAtt=0 if inlist(churchAttendanceW23, 0, 1, 2, 3, 4, 5, 9998)

rename gender FemaleGender

*Generate dummy variables
gen Married=. 
replace Married=1 if inlist(p_maritalW23, 1, 2)
replace Married=0 if inrange(p_maritalW23, 3, 8)

gen Unemployed=. 
replace Unemployed=1 if workingStatusW23==4
replace Unemployed=0 if inlist(workingStatusW23, 1, 2, 3)

egen age_centered = mean(ageW23) 
replace age_centered = ageW23 - age_centered
gen age_centered_squared = age_centered^2

gen Graduate=.
replace Graduate=0 if inrange(p_edlevelUniW23, 0, 3)
replace Graduate=1 if inrange(p_edlevelUniW23, 4, 5)

//Standardize
rename Age Age1
egen Age = std(age_centered_squared)
egen Income = std(p_gross_householdW23)
egen LibValues = std(reversed_al_scaleW23) 
egen SocJusValues = std(socJusValues) 

//Regressions
regress LifeSat SocJusValues [pweight= wt_new_W23], robust 
eststo
regress LifeSat SocJusValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight= wt_new_W23], robust 
eststo
regress LifeSat LibValues [pweight= wt_new_W23], robust 
eststo
regress LifeSat LibValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight= wt_new_W23], robust 
eststo
regress LifeSat SocJusValues LibValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight= wt_new_W23], robust 
eststo
esttab

log close

eststo clear

esttab using "Appendix3.3.rtf", ///
    b(3) se(3) ///
    star(* 0.05 ** 0.01 *** 0.001) ///
    coeflabels(_cons "Constant") ///
    nonumbers replace

// Alternative specification
ologit LifeSat SocJusValues Age FemaleGender Graduate Income Married ReligiousAtt Unemployed [pweight= wt_new_W23], robust 


